/**
 * 
 */
package com.xinghou.platform.dao.bases;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.ParameterDisposer;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

/**
 * @author wxtdy
 *
 */
public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate{
	
	/**
	 * 
	 * @param sql
	 * @param params
	 * @return 主键值
	 */
	public int save(final String sql, final Object... params) {         
        final KeyHolder key = new GeneratedKeyHolder();
        
        update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
                PreparedStatementSetter pss = newArgPreparedStatementSetter(params);
                try {
                    if (pss != null) {
                        pss.setValues(ps);
                    }
                } finally {
                    if (pss instanceof ParameterDisposer) {
                        ((ParameterDisposer) pss).cleanupParameters();
                    }
                }
                return ps;
            }
        }, key);
         
        return key.getKey().intValue();
    }
	
	public int savenukey(final String sql, final Object... params) {         
       // final KeyHolder key = new GeneratedKeyHolder();
        
        update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
                PreparedStatementSetter pss = newArgPreparedStatementSetter(params);
                try {
                    if (pss != null) {
                        pss.setValues(ps);
                    }
                } finally {
                    if (pss instanceof ParameterDisposer) {
                        ((ParameterDisposer) pss).cleanupParameters();
                    }
                }
                return ps;
            }
        });
         
        return 0;
    }


	public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException {
		try {
			return super.queryForObject(sql, rowMapper);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}	
	}
	
	public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
			throws DataAccessException {
		try {
			return super.queryForObject(sql, args, argTypes, rowMapper);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
		
	}

	public <T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException {
		try {
			return super.queryForObject(sql, args, rowMapper);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {
		try {
			return super.queryForObject(sql, rowMapper, args);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
}
